<template>
  <div class="login-container">
    <lang-select class="set-language" width="100%"/>
    <!-- <h3 class="title">HYDRA特殊金额上分后台</h3> -->
    <div class="logo-wrapper title">
        <img class="login-logo" src="./../../assets/images/hydralogo.png">
    </div>
    <transition name="fade" mode="out-in">
      <el-form v-show="showLogin" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left" label-width="0px"
               class="card-box login-form">
        <el-form-item prop="nickname">
        <span class="svg-container svg-container_login">
          <i class="icon iconfont icon-mail_fill"></i>
        </span>
          <el-input name="nickname" type="nickname" v-model="loginForm.nickname" autoComplete="on" :placeholder="$t('login.username')" />
        </el-form-item>
        <el-form-item prop="password">
        <span class="svg-container">
          <i class="icon iconfont icon-lock_fill"></i>
        </span>
          <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on" :placeholder="$t('login.password')"></el-input>
          <span :class="['show-pwd icon iconfont', close?'icon-browse':'icon-Close']"  @click="showPwd"></span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginbtn" style="width:100%;" :loading="loading" @click.native="handleLogin">
            {{$t('login.login')}}
          </el-button>
        </el-form-item>
        <div class="tips">
          <el-button type="text" style="float: right;" @click.native="toReg">{{$t('login.clicktoreg')}}</el-button>
        </div>
      </el-form>
    </transition>
  
    <transition name="fade" mode="out-in">
      <el-form v-show="showReg" autoComplete="on" :model="RegForm" :rules="RegRules" ref="RegForm" label-position="left" label-width="0px"
               class="card-box login-form">
        <el-form-item prop="nickname">
        <span class="svg-container svg-container_login">
          <i class="icon iconfont icon-mine"></i>
        </span>
          <el-input name="username" type="username" v-model="RegForm.nickname" autoComplete="on" :placeholder="$t('login.nickname')" />
        </el-form-item>
        <el-form-item prop="password">
        <span class="svg-container">
          <i class="icon iconfont icon-lock"></i>
        </span>
          <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="RegForm.password" autoComplete="on"
                    :placeholder="'6～20' + $t('login.characters')"></el-input>
          <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
        </el-form-item>
        <el-form-item prop="repassword">
        <span class="svg-container">
          <i class="icon iconfont icon-lock_fill"></i>
        </span>
          <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="RegForm.repassword" autoComplete="on"
                    :placeholder="$t('login.retypepw')"></el-input>
          <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginbtn" style="width:100%;" :loading="loading" @click.native="handleReg">
            {{$t('login.registered')}}
          </el-button>
        </el-form-item>
        <div class="tips">
          <el-button type="text" style="float: right;" @click.native="toLogin">{{$t('login.login')}}</el-button>
        </div>
      </el-form>
    </transition>
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect'

export default {
  components: { LangSelect },
  name: 'login',
  data() {
    const validatePass = (rule, value, callback) => {
      if(value){
        if (value.length < 5) {
          callback(new Error('*密码不能小于5位'))
        } else {
          callback()
        }
      }
    }
    const validaterePass = (rule, value, callback) => {
      if (value === this.RegForm.password){
        callback()
      } else {
        callback(new Error('*两次密码不一致'))
      }
    }
    const validateNickname = (rule, value, callback) => {
      if(value){
        if (value.length <= 0) {
          callback(new Error('*请输入正确的昵称'))
        } else {
          callback()
        }
      }
    }
    return {
      loginForm: {
        nickname: '',
        password: ''
      },
      close: false,
      RegForm: {},
      loginRules: {
        nickname: [{ required: true, trigger: 'blur', validator: validateNickname }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      RegRules: {
        nickname: [{ required: true, trigger: 'blur', validator: validateNickname }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }],
        repassword: [{ required: true, trigger: 'blur', validator: validaterePass }]
      },
      loading: false,
      pwdType: 'password',
      showLogin: true,
      showReg: false
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
      this.close = !this.close
    },
    toReg(){
      this.showLogin = false
      this.showReg = true
      this.RegForm = {}
    },
    toLogin(){
      this.showLogin = true
      this.showReg = false
      this.loginForm = {}
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: '/' })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleReg() {
      this.$refs.RegForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Register', this.RegForm).then((res) => {
            if (res.status === 0){
              this.loading = false
              this.$alert('注册成功，请联系管理人员激活账号！', '注册成功', {
                confirmButtonText: '确定',
                callback: () => {
                  this.toLogin()
                }
              })
            }
          }).catch(() => {
            this.loading = false
          })
        }
      })
      console.log('reg')
    }
  }
}

export const enLogin = {
  username: 'username',
  password: 'password',
  login: 'login',
  clicktoreg: 'click to register',
  nickname: 'nickname',
  characters: 'characters',
  retypepw: 're-type password',
  registered: 'registered'
}

export const zhLogin = {
  username: '用户名',
  password: '密码',
  login: '登录',
  clicktoreg: '点击注册',
  nickname: '中英文均可',
  characters: '个字符',
  retypepw: '重新键入密码',
  registered: '注册'
}
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .login-container {
    position: fixed;
    height: 100%;
    width:100%;
    background: $bg;
    .title {
    }
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 40px;
      font-weight: 400;
      color: $light_gray;
      margin: 200px auto 0px auto;
      text-align: center;
      font-weight: bold;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
      margin: auto auto;
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .iconfont {
      font-size: 23px;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select:none;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
